// pages/login/login.js
const app = getApp();
const login = require('../../config/login.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    password: '',
    list: [{
      id: 1,
      name: "记住账号密码",
      value: false
    }],
  },
  inpAccount(e) {
    const that = this;
    that.setData({
      account: e.detail.value
    })
  },
  inpPassword(e) {
    const that = this;
    that.setData({
      password: e.detail.value
    })
  },
  checkboxChange(e) {
    if (e.detail.value.length > 0) {
      wx.setStorage({
        key: 'ischeck',
        data: true
      })
    } else {
      wx.setStorage({
        key: 'ischeck',
        data: false
      })
    }
  },

  bindGetUserInfo() {
    const that = this;
    if (that.data.account) {
      if (that.data.password) {
        let json = {
          username: that.data.account,
          password: that.data.password
        }
        wx.showLoading({
          title: '登录中...',
        })
        login.myAuthorize(json, () => {
          wx.setStorage({
            key: 'acount',
            data: that.data.account
          })

          wx.setStorage({
            key: 'password',
            data: that.data.password
          })

          wx.hideLoading();
          wx.switchTab({
            url: '/pages/index/index',
          })
        })
      } else {
        wx.showToast({
          title: '请输入密码',
          icon: 'error',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '请输入账号',
        icon: 'error',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.getStorage({
      key: 'ischeck',
      success: function (res) {
        var lists = that.data.list;
        lists[0].value = res.data;
        that.setData({
          list: lists
        })
        if (res.data) {
          wx.getStorage({
            key: 'acount',
            success: function (res) {
              that.setData({
                account: res.data
              })

            }
          })

          wx.getStorage({
            key: 'password',
            success: function (res) {
              that.setData({
                password: res.data
              })
            }
          })
        }else{
          wx.setStorage({
            key: 'acount',
            data: ''
          })

          wx.setStorage({
            key: 'password',
            data: ''
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})